//
//  LKCustomKeyBoardView.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/7/7.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKCustomKeyBoardView.h"

@implementation LKCustomKeyBoardView


-(instancetype)initWithFrame:(CGRect)frame AndreturnTitle:(NSString *)returnStr{
    if (self = [super initWithFrame:frame]) {
        NSArray * titlesArr = @[@"7",@"8",@"9",@"删除",@"4",@"5",@"6",@"C",@"1",@"2",@"3",returnStr,@"0",@"."];
            self.backgroundColor = COLOR_HEX(0x999999);
            self.frame = frame;
            NSInteger totalLoc = 4;
            CGFloat margin = 1;
            CGFloat W = (self.size.width - (totalLoc - 1) *margin)/totalLoc;
            CGFloat H = W;
        //    CGFloat margin=(view.frame.size.width-totalLoc * W)/(totalLoc+1);
            for (NSInteger i = 0; i < titlesArr.count; i++) {
                UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setTitle:titlesArr[i] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                btn.backgroundColor = [UIColor whiteColor];
                btn.titleLabel.font = [UIFont systemFontOfSize:26];
                /*计算frame*/
                NSInteger row = i / totalLoc;//行号
                NSInteger loc = i % totalLoc;//列号
                //0/3=0,1/3=0,2/3=0,3/3=1;
                //0%3=0,1%3=1,2%3=2,3%3=0;
                CGFloat X= (margin + W) * loc;
                CGFloat Y= margin + (margin + H) * row;
                btn.frame = CGRectMake(X, Y, W, H);
                if ([titlesArr[i] isEqualToString:returnStr]) {
                    btn.frame = CGRectMake(X, Y, W, H * 2);
                    btn.backgroundColor = [UIColor redColor];
                    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    btn.titleLabel.font = [UIFont systemFontOfSize:36];
                }
                if ([titlesArr[i] isEqualToString:@"."]) {
                    btn.frame = CGRectMake(X * 2, Y, W, H);
                }
                if ([titlesArr[i] isEqualToString:@"0"]) {
                    btn.frame = CGRectMake(X, Y, W * 2, H);
                }
                if ([titlesArr[i] isEqualToString:@"删除"]) {
                    [btn setTitle:@"" forState:UIControlStateNormal];
                    [btn setImage:[UIImage imageNamed:@"ic_backspace"] forState:UIControlStateNormal];
                }
                //设置tag值(这里的tag，只是为了让button的每次点击都有不同的动画效果)
                btn.tag = 1000 + i;
                
                [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
                
                [self addSubview:btn];
            }
    }
    return self;
}

-(void)clickBtn:(UIButton *)btn{

    NSInteger tag = (NSInteger)btn.tag - 1000;
    if (tag == 3) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(lkClickDelete:)]) {
            [self.delegate lkClickDelete:btn];
        }
        return;
    }
    if (tag == 7) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(lkClickClear:)]) {
            [self.delegate lkClickClear:btn];
        }
        return;
    }
    if (tag == 11) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(lkClickdetermine:)]) {
            [self.delegate lkClickdetermine:btn];
        }
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(lkClickConten:)]) {
        [self.delegate lkClickConten:btn];
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
